package com.example.day15;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class MyService2 extends Service {
    int startId=0;
    Handler handler =new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Log.e("handler#####",msg.arg1+"");
            MyService2.this.stopSelf();
        }
    };

    public class mythread extends Thread{
        public void run(){
            Log.e("###", "run");
            for(int i = 0; i<100; i++)
            {
//                Log.e("###", Thread.currentThread().getName() + "i =  " + i);
                Message msg=new Message();
                msg.arg1=i;
                handler.sendMessage(msg);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    public MyService2() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.startId = startId;
        new mythread().start();
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("###", "onDestroy");
    }
}
